/*-
 * Copyright 2012 Guram Dukashvili
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
//---------------------------------------------------------------------------
#ifndef __thread_h__
#define __thread_h__
//---------------------------------------------------------------------------
#include "config.h"
#include "vector.h"
#include "rwlock.h"
//---------------------------------------------------------------------------
struct thread_pool;
//---------------------------------------------------------------------------
struct thread {
  union {
    void (* entryf)(struct thread * thr,void * object,uintptr_t param);
    void * entry;
  };

  void * object;
  uintptr_t param;

#if __WIN32__
  HANDLE handle;
  DWORD tid;
#elif HAVE_PTHREAD_H
  pthread_t handle;
#else
#error must be defined for arch
#endif

  struct semaphore started;
  struct semaphore restart;
  struct semaphore finished;

  struct thread_pool * pool;
  uintptr_t pool_index;
  bool pool_used;

  int exit_code;

  volatile bool terminate;
  volatile bool destroy;
};
//---------------------------------------------------------------------------
void thread_init_runtime(void);
void thread_destroy_runtime(void);
struct thread * thread_new(void);
void thread_init(struct thread * thr);
void thread_destroy(struct thread * thr);
void thread_delete(struct thread * thr);
void thread_start(struct thread * thr);   // reenterable
void thread_wait(struct thread * thr);    // not reenterable
bool thread_active(struct thread * thr);
//---------------------------------------------------------------------------
struct thread_pool {
  struct mutex lock;
  vector pool;
  uintptr_t max_size;
  uintptr_t cores;
  uintmax_t new_threads_count;
  uintmax_t reused_threads_count;
};
//---------------------------------------------------------------------------
struct thread_pool * thread_pool_new(void);
void thread_pool_init(struct thread_pool * pool);
void thread_pool_destroy(struct thread_pool * pool);
void thread_pool_delete(struct thread_pool * pool);
void thread_pool_wait(struct thread_pool * pool);
struct thread * thread_pool_spare(struct thread_pool * pool,void * entry,void * object,uintptr_t param);
//---------------------------------------------------------------------------
void thread_locking_test(uintptr_t rthreads,uintptr_t wthreads,uintptr_t it);
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
